/**
 * 
 */
package com.wisdytech.linkdcs.equipment.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqClientDao;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.dao.IEqToClientDao;
import com.wisdytech.linkdcs.equipment.model.Client;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.equipment.model.EquipmentToClient;
import com.wisdytech.linkdcs.equipment.service.ICollectTerminalService;

/**
 * <p>
 * ClassName: CollectTerminalServiceImpl
 * </p>
 * <p>
 * Description: 采集终端管理页面的业务实现层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-29
 * </p>
 */
@Service("collectTerminalService")
@Transactional
public class CollectTerminalServiceImpl extends BaseService<IEqClientDao, Client> implements ICollectTerminalService{
    /**
     * 注入采集终端dao
     */
    @Resource
    private IEqClientDao clientDao;
    
    /**
     * 注入设备dao
     */
    @Resource
    private IEqEquipmentDao equipmentDao;
    /**
     * 设备与终端关联dao
     */
    @Resource
    private IEqToClientDao iEqToClientDao;
    
    @Override
    public PageInfo<Client> listCollectTerminalByFilter(String clientCode, String clientName, String operationId, Filter filter) {
    	
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        queryMap.put("clientCode", clientCode);
        queryMap.put("operationId", operationId);
        queryMap.put("clientName", clientName);
        List<Client> lists = super.beforeList(IEqClientDao.class, filter).selectCollectPage(queryMap);
        PageInfo<Client> collectTerminalPage=new PageInfo<>(lists);
        
        return collectTerminalPage;
    }

    @Override
    public StandardResult addCollectTerminal(Client client, String loginName) {
        EntityWrapper<Client> ew =new EntityWrapper<>();
        //验证采集终端编号的唯一性
        List<Client> clientList;
        clientList = clientDao.selectPage(new Page<Client>(1, 1), ew.eq("client_code", client.getClientCode()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!clientList.isEmpty()) {
            return StandardResult.getFailResult(CommonConstants.COLLECTION_TERMINAL);
        }
        client.setPkId(BaseUtil.getUUID());
        client.setCreateDate(new Date());
        client.setCreateUser(loginName);
        client.setOperationId(client.getOperationName());
        client.setDelFlag(CommonConstants.DEL_FLAG);
        int count = clientDao.insert(client);
        if (count <=0){
            throw new ServiceException("新增失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateCollectTerminal(Client client, String loginName) {
        EntityWrapper<Client> ew =new EntityWrapper<>();
        //验证采集终端编号的唯一性
        List<Client> clientList;
        clientList = clientDao.selectPage(new Page<Client>(1, 1), ew.eq("client_code", client.getClientCode()).ne("pk_id", client.getPkId()));
        if (clientList.size() > 0){
            return StandardResult.getFailResult(CommonConstants.COLLECTION_TERMINAL);
        }
        client.setUpdateDate(new Date());
        client.setUpdateUser(loginName);
        client.setOperationId(client.getOperationName());
        int count = clientDao.updateById(client);
        if (count<=0) {
            throw new ServiceException("修改失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public Client getCollectTerminalById(String pkId) {
        return clientDao.getCollectTerminalById(pkId);
    }

    @Override
    public StandardResult delCollectTerminalById(String id) {
        List<EquipmentToClient> ewEquipmentToClientList = new ArrayList<EquipmentToClient>();
        List<String> idList = new ArrayList<String>();
        String[] ids = id.split(",");
        for (int i = 0; i < ids.length; i++) {
            idList.add(ids[i]);
        }
//        for (String pkId : ids) {
//            EntityWrapper<Equipment> ewEquipment = new EntityWrapper<>();
//            equipmentList = equipmentDao.selectList(ewEquipment.eq("EQUIP_CLIENT_ID", pkId));
//            if (equipmentList.size() >= 1) {
//                return StandardResult.getFailResult(CommonConstants.COLLECTION_TERMINAL_USE);
//            }
//        }
        
	      for (String pkId : ids) {
	      EntityWrapper<EquipmentToClient> ewEquipmentToClient = new EntityWrapper<>();
	      ewEquipmentToClientList = iEqToClientDao.selectList(ewEquipmentToClient.eq("CLIENT_ID", pkId));
	      for(EquipmentToClient ewEToClient : ewEquipmentToClientList) {
		      if (ewEToClient.getEquipCode()!=null || ewEToClient.getEquipCode() != "") {
		          return StandardResult.getFailResult(CommonConstants.COLLECTION_TERMINAL_USE);
		      }
	      }

	  }
        int count = clientDao.deleteBatchIds(idList);
        if (count <= 0) {
            throw new ServiceException("删除失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public List<Client> listAllCollectTerminal() {
        return clientDao.listAllCollectTerminal();
    }

    @Override
    public Client getCollectTerminalByClientCode(String clientCode) {
        Client client = clientDao.getCollectTerminalByClientCode(clientCode);
        return client;
    }

   
    

}
